package com.financing.platform.web.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.SwitchDTO;
import com.financing.platform.service.ISwitchService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;

@Controller("switchActionApi")
@RequestMapping("/api/switch")
@Scope("prototype")
public class SwitchAction extends BaseAction{
	
	@Autowired
	ISwitchService switchSerivceImpl;
	
	@RequestMapping("/querySwitch.shtml")
	@ResponseBody
	public ResultMap querySwitch(HttpServletRequest request,
			HttpServletResponse response) {
		try{
			List<SwitchDTO> switchList = switchSerivceImpl.queryAllSwitch();
			resultMap.setAttribute("list", switchList);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("query switch error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
	}
	
	@RequestMapping("/updateSwitch.shtml")
	@ResponseBody
	public ResultMap updateSwitch(HttpServletRequest request,
			HttpServletResponse response,SwitchDTO switchDto) {
		
			try{
				DTOdateSet.setUpdateInfo(switchDto, request.getSession());
				if(Dict.switchOff.equals(switchDto.getsValue())|| Dict.switchOn.equals(switchDto.getsValue())){
					 switchSerivceImpl.updateSwitch(switchDto);
				}
				resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}catch(Exception e){
				log.error("update switch error!",e);
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			return resultMap;
	}
}
